/**
 * 
 */

function validateLoginForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var username = document.forms["loginform"]["username"].value;
	var password = document.forms["loginform"]["password"].value;
	var textError = null;
	var check = true;
	if (username == null || username == "") {
		textError = "Username must be filled out";
		addElements(div, textError);
		check = false;
	}
	if (password == null || password == "") {
		textError = "Password must be filled out";
		addElements(div, textError);
		check = false;
	}
	if (username != null && username != "" && password != null
			&& password != "" && checkExistedAccountAdmin() == false) {
		textError = "Username or Password is wrong!";
		addElements(div, textError);
		check = false;
	}
	return check;
}

function addElements(div, textError) {
	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);
	var text = document.createTextNode(textError);
	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedAccountAdmin() {
	var flag = false;
	$.ajax({
		"type" : "GET",
		"async" : false,
		"url" : "/core-web-admin/adminaccount/checkexistedaccountadmin",
		"data" : {
			"username" : $('#username').val(),
			"password" : $('#password').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED ACCOUNT ADMIN!");
			flag = false;
		}
	});
	return flag;
}